from flask import Flask, render_template, request, escape
import os
path_file = os.path.dirname(__file__)

a = os.path.dirname(os.path.abspath(__file__))
os.chdir(a)

app = Flask(__name__)

#广东菜
def read_file() -> None:
    with open('gd.csv', 'r', encoding='gb18030') as log:
        print(username, lastname, firstname, id, github, segmentfault, email, file=log, sep='|')

def file():
    contents=[]
    with open('gd.csv', encoding='gb18030') as log:
        for line in log:
            contents.append([])
            for item in line.split(','):
                contents[-1].append(escape(item))
        return contents

#火锅店
def read_file1() -> None:
    with open('hg.csv', 'r', encoding='gb18030') as log:
        print(username, lastname, firstname, id, github, segmentfault, email, file=log, sep='|')

def file1():
    contents=[]
    with open('hg.csv', encoding='gb18030') as log:
        for line in log:
            contents.append([])
            for item in line.split(','):
                contents[-1].append(escape(item))
        return contents

#茶餐厅
def read_file2() -> None:
    with open('c.csv', 'r', encoding='gb18030') as log:
        print(username, lastname, firstname, id, github, segmentfault, email, file=log, sep='|')

def file2():
    contents=[]
    with open('c.csv', encoding='gb18030') as log:
        for line in log:
            contents.append([])
            for item in line.split(','):
                contents[-1].append(escape(item))
        return contents
#地方特色
def read_file3() -> None:
    with open('df.csv', 'r', encoding='gb18030') as log:
        print(username, lastname, firstname, id, github, segmentfault, email, file=log, sep='|')

def file3():
    contents=[]
    with open('df.csv', encoding='gb18030') as log:
        for line in log:
            contents.append([])
            for item in line.split(','):
                contents[-1].append(escape(item))
        return contents

#潮州菜
def read_file4() -> None:
    with open('cz.csv', 'r', encoding='gb18030') as log:
        print(username, lastname, firstname, id, github, segmentfault, email, file=log, sep='|')

def file4():
    contents=[]
    with open('cz.csv', encoding='gb18030') as log:
        for line in log:
            contents.append([])
            for item in line.split(','):
                contents[-1].append(escape(item))
        return contents

#外国餐厅
def read_file5() -> None:
    with open('wg.csv', 'r', encoding='gb18030') as log:
        print(username, lastname, firstname, id, github, segmentfault, email, file=log, sep='|')

def file5():
    contents=[]
    with open('wg.csv', encoding='gb18030') as log:
        for line in log:
            contents.append([])
            for item in line.split(','):
                contents[-1].append(escape(item))
        return contents

def what(name):
    contents = file()
    for line in contents:
        for item in line:
            if name != item:
                continue
            else:
                return line

@app.route('/shimie')
def entry_page() -> 'html':
    return render_template('go.html',
                           the_title='食咩')

@app.route('/finding', methods=['POST'])
def do_search() -> 'html':
    name = request.form['username']
    letters = request.form['lastname']
    results = what(name)
    title = '搜索&结果:'
    titles = ('','商户', '商区', '评分', '人均消费','地址','联系')
    return render_template('ending.html', 
                           the_phrase=name,
                           the_letters=letters,
                           the_title=title,
                           the_row_titles=titles,
                           the_results=results,
                           )


@app.route('/gd', methods=['POST'])
def read_file() -> 'html':
    contents = file()
    titles = ('','商户', '地址', '商区', '评分','人均消费')            
    return render_template('gd.html',
                           the_title='广东菜',
                           the_row_titles=titles,
                           the_data=contents,)

@app.route('/hg', methods=['POST'])
def read_file1() -> 'html':
    contents = file1()
    titles = ('','商户', '商区', '评分', '人均消费','地址','联系')            
    return render_template('hg.html',
                           the_title='火锅',
                           the_row_titles=titles,
                           the_data=contents,)

@app.route('/c', methods=['POST'])
def read_file2() -> 'html':
    contents = file2()
    titles = ('','商户', '商区', '评分', '人均消费','地址','联系')            
    return render_template('c.html',
                           the_title='茶餐厅',
                           the_row_titles=titles,
                           the_data=contents,)

@app.route('/df', methods=['POST'])
def read_file3() -> 'html':
    contents = file3()
    titles = ('','商户', '商区', '评分', '人均消费','地址','联系')            
    return render_template('df.html',
                           the_title='地方特色',
                           the_row_titles=titles,
                           the_data=contents,)

@app.route('/cz', methods=['POST'])
def read_file4() -> 'html':
    contents = file4()
    titles = ('','商户', '商区', '评分', '人均消费','地址','联系')            
    return render_template('cz.html',
                           the_title='潮州菜',
                           the_row_titles=titles,
                           the_data=contents,)


@app.route('/wg', methods=['POST'])
def read_file5() -> 'html':
    contents = file5()
    titles = ('','商户', '商区', '评分', '人均消费','地址','联系','类别')            
    return render_template('wg.html',
                           the_title='外国餐厅',
                           the_row_titles=titles,
                           the_data=contents,)


if __name__ == '__main__':
    app.run(debug=True)
